package com.flyan.game.motafx.core.algorithm;

import com.almasb.fxgl.entity.Entity;
import com.flyan.game.motafx.component.LifePanelComponent;
import com.flyan.game.motafx.core.consts.PropertyConsts;
import com.flyan.game.motafx.core.panel.GameInfoPanel;
import com.flyan.game.motafx.core.pojo.LifePanel;
import com.flyan.game.motafx.core.util.entity.EntityUtils;

/**
 * 战斗人员
 *
 * @author flyan
 * @version 1.0
 * @email flyan_by_chenu@sina.com
 * @since 2025/2/5
 */
public class Battler {

    /**
     * 参与战斗的实体
     */
    private final Entity entity;

    /**
     * 生命实体面板
     */
    private LifePanel lifePanel;

    public Battler(Entity entity) {
        this.entity = entity;
        initLifePanel();
    }

    public void initLifePanel() {
        LifePanel lp =  EntityUtils.getEntityProperty(entity, PropertyConsts.LIFE_PANEL, LifePanel.class);
        if (lp != null) {
            this.lifePanel = lp.clone();
        }
    }

    /**
     * 战斗结果生效，将新的生命面板更新到实体上
     */
    public void effect() {
        if (lifePanel != null) {
            LifePanelComponent lifePanelComponent = entity.getComponent(LifePanelComponent.class);
            lifePanelComponent.setLifePanel(lifePanel);
            GameInfoPanel.redraw();
        }

        /* 死亡？消失吧！ */
        if (lifePanel == null || lifePanel.isDead()) {
            entity.removeFromWorld();
        }
    }

    public Entity getEntity() {
        return entity;
    }

    public LifePanel getLifePanel() {
        return lifePanel;
    }

}
